package com.dm0858.bianmin.mine.tools;

import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.util.Log;

import androidx.annotation.ColorInt;

/**
 * @description 使用语义化的方式定义一些控件的基本形状
 * @author 迪迪丶Sir
 * @e-mail asd969709719@qq.com
 * @since 2020-03-16
 **/
public class ShapeUtils {

    //默认线条的粗细
    private static final int defaultLineWidth = 1;
    //默认线条的颜色
    private static final int defaultLineColor = Color.GRAY;
    //默认圆角的半径
    private static final int defaultCornerRadius = 2;
    //默认的椭圆形圆角半径
    private static final int defaultRoundCornerRadius = 100;
    //默认虚线线条单位长度
    private static final int defaultDashWidth = 6;
    //默认虚线线条间隔
    private static final int defaultDashGap = 2;
    //初始形状为矩形
    private int shape = GradientDrawable.RECTANGLE;
    //初始线宽
    private int lineWidth = 0;
    //初始圆角半径
    private float cornerRadius = 2;
    //初始虚线边框每个单元的长度
    private float dashWidth = 0;
    //初始虚线边框每个单元的间隔
    private float dashGap = 0;
    //初始线条颜色
    @ColorInt
    private int lineColor = Color.TRANSPARENT;
    //初始背景颜色
    @ColorInt private int backgroundColor = Color.TRANSPARENT;

    public ShapeUtils() {

    }

    /**
     * @description 生成需要的形状
     * @param
     * @return
     * @author 迪迪丶Sir
     * @since 2020-03-16
     **/
    public Drawable build() {
        final GradientDrawable drawable = new GradientDrawable();
        drawable.setShape(shape);
        drawable.setStroke(lineWidth,lineColor,dashWidth,dashGap);
        Log.e("ShapeUtils", "build: " + cornerRadius );
        drawable.setCornerRadius(cornerRadius);
        drawable.setColor(backgroundColor);
        return drawable;
    }
    
    /**
     * @description 设置形状
     * @param
     * @return
     * @author 迪迪丶Sir
     * @since 2020-03-16
     **/
    public ShapeUtils shape(int shape) {
        this.shape = shape;
        return this;
    }

    /**
     * @description 默认生成一个 1dp 灰色的线
     * @param
     * @return
     * @author 迪迪丶Sir
     * @since 2020-03-16
     **/
    public ShapeUtils line() {
        return line(defaultLineWidth,defaultLineColor);
    }

    /**
     * @description 构造线框
     * @param
     * @return
     * @author 迪迪丶Sir
     * @since 2020-03-16
     **/
    public ShapeUtils line(final int width,@ColorInt final int color) {
        return lineWidth(width).lineColor(color);
    }

    /**
     * @description 设置边框线条的粗细，传入具体数值
     * @param
     * @return
     * @author 迪迪丶Sir
     * @since 2020-03-16
     **/
    public ShapeUtils lineWidth(final int lineWidth) {
        this.lineWidth = ConversionUtils.dp2px(lineWidth);
        return this;
    }

    /**
     * @description 设置边框线条的颜色
     * @param
     * @return
     * @author 迪迪丶Sir
     * @since 2020-03-16
     **/
    public ShapeUtils lineColor(@ColorInt int lineColor) {
        this.lineColor = lineColor;
        return this;
    }

    /**
     * @description 默认设置圆角的半径为2
     * @param
     * @return
     * @author 迪迪丶Sir
     * @since 2020-03-16
     **/
    public ShapeUtils corner() {
        return corner(defaultCornerRadius);
    }

    /**
     * @description 默认设置大圆角的半径100
     * @param
     * @return
     * @author 迪迪丶Sir
     * @since 2020-03-16
     **/
    public ShapeUtils roundCorner() {
        return corner(defaultRoundCornerRadius);
    }

    /**
     * @description 设置圆角的半径
     * @param
     * @return
     * @author 迪迪丶Sir
     * @since 2020-03-16
     **/
    public ShapeUtils corner(float cornerRadius) {
        this.cornerRadius = ConversionUtils.dp2px(cornerRadius);
        return this;
    }

    /**
     * @description 填充颜色
     * @param
     * @return
     * @author 迪迪丶Sir
     * @since 2020-03-16
     **/
    public ShapeUtils fill(@ColorInt int backgroundColor) {
        this.backgroundColor = backgroundColor;
        return this;
    }

    public ShapeUtils dash() {
        return dashWidth(defaultDashWidth).dashGap(defaultDashGap);
    }

    public ShapeUtils dashWidth(float dashWidth) {
        this.dashWidth = dashWidth;
        return this;
    }

    public ShapeUtils dashGap(float dashGap) {
        this.dashGap = dashGap;
        return this;
    }
























}
